package com.android.test.view;

import java.util.ArrayList;

import com.android.test.R;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;

public class NumberGraph extends LinearLayout {

    private final String TAG = "NumberGraph";
    private int number = 0;
    private ImageView hundredNumber;
    private ImageView decibalNumber;
    private ImageView singleNumber;
    private Context mContext;
    private ArrayList<Drawable> iconList = new ArrayList<Drawable>();
    private final int MAX_VALUE = 100;
    private final int NUMBER_STEP = 4;

    public NumberGraph(Context context) {
	super(context);
	init(context, null);
    }

    public NumberGraph(Context context, AttributeSet attrs) {
	super(context, attrs);
	init(context, attrs);

	// TODO Auto-generated constructor stub
    }

    private void init(Context context, AttributeSet attrs) {
	// TODO Auto-generated method stub
	mContext = context;
	readIconFromSource();
	setOrientation(HORIZONTAL);
	LayoutParams param = new LayoutParams(context, attrs);
	param.weight = 1;
	param.height = LayoutParams.FILL_PARENT;
	param.width = 0;
	hundredNumber = new ImageView(context);
	decibalNumber = new ImageView(context);
	singleNumber = new ImageView(context);
	addView(hundredNumber, 0, param);
	addView(decibalNumber, 1, param);
	addView(singleNumber, 2, param);
    }

    private void readIconFromSource() {
	iconList.add(mContext.getResources().getDrawable(R.drawable.num_0));
	iconList.add(mContext.getResources().getDrawable(R.drawable.num_1));
	iconList.add(mContext.getResources().getDrawable(R.drawable.num_2));
	iconList.add(mContext.getResources().getDrawable(R.drawable.num_3));
	iconList.add(mContext.getResources().getDrawable(R.drawable.num_4));
	iconList.add(mContext.getResources().getDrawable(R.drawable.num_5));
	iconList.add(mContext.getResources().getDrawable(R.drawable.num_6));
	iconList.add(mContext.getResources().getDrawable(R.drawable.num_7));
	iconList.add(mContext.getResources().getDrawable(R.drawable.num_8));
	iconList.add(mContext.getResources().getDrawable(R.drawable.num_9));
    }

    public void drawNumber(int value, boolean animation) {
	if (value > MAX_VALUE || value < 0) {
	    return;
	}

	hundredNumber.setScaleType(ScaleType.FIT_XY);
	decibalNumber.setScaleType(ScaleType.FIT_XY);
	singleNumber.setScaleType(ScaleType.FIT_XY);

	int hundred = value / 100;
	int decibal = value / 10;
	int single = value % 10;
	if (animation == false) {
	    hundredNumber.setImageDrawable(iconList.get(hundred));
	    decibalNumber.setImageDrawable(iconList.get(decibal));
	    singleNumber.setImageDrawable(iconList.get(single));
	} else {
	    NumberFreshTask task = new NumberFreshTask();
	    task.execute(value);
	}

    }

    class NumberFreshTask extends AsyncTask<Integer, Integer, Integer> {

	@Override
	protected Integer doInBackground(Integer... params) {
	    int currentNumber = 0;
	    while (currentNumber <= number) {
		publishProgress(currentNumber);
		currentNumber ++;//= NUMBER_STEP;

		try {
		    Thread.sleep(10);
		} catch (InterruptedException e) {
		    // TODO Auto-generated catch block
		    e.printStackTrace();
		}
	    }

	    return 0;
	}

	protected void onProgressUpdate(Integer... progress) {
	    drawNumber(progress[0], false);
	}

	protected void onPostExecute(Integer result) {
	    drawNumber(number, false);
	}

	protected void onPreExecute() {
	}

	protected void onCancelled() {
	    super.onCancelled();
	}
    }

    public int getNumber() {
	return number;
    }

    public void setNumber(int number) {
	this.number = number;
    }

}
